<?php
namespace app\common\taglib;

use think\template\TagLib;
use think\Db;
class pc extends TagLib
{
	
    protected $tags = array(
        'link' => ['attr' => 'type,limit,order,return,field,cache', 'close' => 1],
        'linktype' => ['attr' => 'limit,order,return,field,cache', 'close' => 1],
        'banner' => ['attr' => 'type,limit,order,return,field,cache', 'close' => 1],
        'gbook' => ['attr' => 'pagesize,limit,order,return,field,cache', 'close' => 1],
        'article' => ['attr' => 'cid,field,orderby,limit,pagesize,empty','close' => 1],
        'products' => ['attr' => 'cid,field,orderby,limit,pagesize,empty','close' => 1],
        'block' => ['attr' => 'type,cache,return','close' => 1]
    );
    
	
	
    /**
            * 获取友情链接列表
        */
    public function tagLinktype($tag, $content)
    {
        $data['order'] = $order =isset($tag['order']) ? $tag['order'] : 'sort desc';
        $data['cache'] = $cache =isset($tag['cache']) ? $tag['cache'] : '';
        $data['return'] = $return = isset($tag['return']) ? '$' .$tag['return'] : '$data';
        $data['limit'] = $limit = isset($tag['limit']) ? $tag['limit'] : '';
        $data['field'] = $field = isset($tag['field']) ? $tag['field'] : '*';
                
                
        $cacheId = to_guid_string(arr_to_html($data));
        $parse = <<<EOF
				<?php
					   \$__LIST__ = think\Db::name('link');
					   if("$limit" != ''){
							\$__LIST__=\$__LIST__->limit($limit);
					   }
					   if("$order" != ''){
							\$__LIST__=\$__LIST__->order('$order');
					   }
					   if("$cache" != ''){
							\$__LIST__=\$__LIST__->cache('$cacheId','$cache','link');
					   }
					   if("$field" != ''){
							\$__LIST__=\$__LIST__->field('$field');
					   }
					   \$__LIST__=\$__LIST__->select();
						$return = \$__LIST__;
					   ?>
EOF;
        $parse .= $content;
        return $parse;
    }
    
    
    /**
        * 获取友情链接列表
    */
    public function tagLink($tag, $content)
    {
        $data['type'] = $type = $tag['type'];
        $data['order'] = $order =isset($tag['order']) ? $tag['order'] : 'sort desc';
        $data['cache'] = $cache =isset($tag['cache']) ? $tag['cache'] : '';
        $data['return'] = $return = isset($tag['return']) ? '$' .$tag['return'] : '$data';
        $data['limit'] = $limit = isset($tag['limit']) ? $tag['limit'] : '';
        $data['field'] = $field = isset($tag['field']) ? $tag['field'] : '*';
                
                
        $cacheId = to_guid_string(arr_to_html($data));
        $parse = <<<EOF
		        <?php
		               \$__LIST__ = think\Db::name('link_detail')->where(['type'=> $type,'is_check' => 1]);
		               if("$limit" != ''){
		                    \$__LIST__=\$__LIST__->limit($limit);
		               }
					   if("$order" != ''){
						    \$__LIST__=\$__LIST__->order('$order');
					   }
					   if("$cache" != ''){
					   		\$__LIST__=\$__LIST__->cache('$cacheId','$cache','link_detail');
					   }
					   if("$field" != ''){
					   		\$__LIST__=\$__LIST__->field('$field');
					   }
		               \$__LIST__=\$__LIST__->select();
		                $return = \$__LIST__;
		               ?>
EOF;
        $parse .= $content;
        return $parse;
    }
    /**
    * 获取首页轮播图
    */
    public function tagBanner($tag, $content)
    {
        $data['type'] = $type = $tag['type'];
        $data['order'] = $order =isset($tag['order']) ? $tag['order'] : 'sort desc';
        $data['cache'] = $cache =isset($tag['cache']) ? $tag['cache'] : '';
        $data['return'] = $return = isset($tag['return']) ? '$' .$tag['return'] : '$data';
        $data['limit'] = $limit = isset($tag['limit']) ? $tag['limit'] : '';
        $data['field'] = $field = isset($tag['field']) ? $tag['field'] : '*';
                
                
        $cacheId = to_guid_string(arr_to_html($data));
        $parse = <<<EOF
		        <?php
		               \$__LIST__ = think\Db::name('banner_detail')->where(['type'=> $type,'is_check' => 1]);
		               if("$limit" != ''){
		                    \$__LIST__=\$__LIST__->limit($limit);
		               }
					   if("$order" != ''){
						    \$__LIST__=\$__LIST__->order('$order');
					   }
					   if("$cache" != ''){
					   		\$__LIST__=\$__LIST__->cache('$cacheId','$cache','banner_detail');
					   }
					   if("$field" != ''){
					   		\$__LIST__=\$__LIST__->field('$field');
					   }
		               \$__LIST__=\$__LIST__->select();
					   $return = \$__LIST__;
		               ?>
EOF;
        $parse .= $content;
        return $parse;
    }
    
    /**
         * 获取文章列表
         */
    public function tagArticle($tag, $content)
    {
        $data['cid'] = $cid = isset($tag['cid']) ? $tag['cid'] : 0;
        $data['cache'] = $cache = isset($tag['cache']) ? $tag['cache']:'';
        $data['order'] = $order = isset($tag['orderby']) ? $tag['orderby'] : '';
        $data['limit'] = $limit = isset($tag['limit']) ? $tag['limit'] : 10;
        $data['pagesize'] = $pagesize = isset($tag['pagesize']) ? $tag['pagesize'] : 0;
        $data['field'] = $field = isset($tag['field']) ? $tag['field'] : '';
        $data['empty'] = $empty = isset($tag['empty']) ? $tag['empty'] : '';
		$data['return'] = $return = isset($tag['return']) ? '$'.$tag['return'] : '$data';
        $cacheId = to_guid_string(arr_to_html($data));
        $parse = <<<EOF
	        <?php
	                \$condition['status'] = 1;
	                /*存在子类，则获取子类下的文章*/
	                if($cid != 0){
	                    \$condition['cid'] = $cid;
						\$children_ids = think\Db::name('category')->field('id')->where('pid',\$condition['cid'])->select();
	                    \$children_ids = array_column(\$children_ids,'id');
						\$children_ids[] = $cid;
						
	                }
					\$list = think\Db::name('news')->where(\$condition);
	                
	                
	                if(isset(\$children_ids) && \$children_ids){
	                     \$children_ids = implode(',',\$children_ids);
	                     \$list = \$list->whereOr("cid in (\$children_ids) and status=1");
	                     unset(\$children_ids);//释放变量
	                }
	                if("$field" != ''){
	                    \$list=\$list->field("$field");
	                }
	                if("$order" != ''){
	                    \$list=\$list->order("$order");
	                }
	                if("$pagesize" > 0){
	                    \$list=\$list->paginate("$pagesize");
	                    \$page = \$list->render();
	                }else{
	                    if("$limit" != 0){
							\$list=\$list->limit("$limit");
	                    }
	                    \$list=\$list->select();
	                }
	                \$__LIST__ = \$list;
	                foreach(\$__LIST__ as \$key => {$return}):
	                {$return}['url'] = url('index/show/index',['cid' => {$return}['cid'],'aid' => {$return}['aid']]);
	                ?>
EOF;
        $parse .= $content;
        $parse .= '<?php endforeach;?>';
        return $parse;
    }
	/**
         * 获取产品列表
         */
    public function tagProducts($tag, $content)
    {
        $data['cid'] = $cid = isset($tag['cid']) ? $tag['cid'] : 0;
        $data['cache'] = $cache = isset($tag['cache']) ? $tag['cache']:'';
        $data['order'] = $order = isset($tag['orderby']) ? $tag['orderby'] : '';
        $data['limit'] = $limit = isset($tag['limit']) ? $tag['limit'] : 10;
        $data['pagesize'] = $pagesize = isset($tag['pagesize']) ? $tag['pagesize'] : 0;
        $data['field'] = $field = isset($tag['field']) ? $tag['field'] : '';
        $data['empty'] = $empty = isset($tag['empty']) ? $tag['empty'] : '';
		$data['return'] = $return = isset($tag['return']) ? '$'.$tag['return'] : '$data';
        $cacheId = to_guid_string(arr_to_html($data));
        $parse = <<<EOF
	        <?php
	                /*存在子类，则获取子类下的文章*/
	                if($cid != 0){
	                    \$condition['cid'] = $cid;
						\$children_ids = think\Db::name('category')->where('pid',$cid)->column('id');
						\$children_ids[] = $cid;
						\$children_ids = implode(',',\$children_ids);
						
						
	                }
	                if(isset(\$children_ids) && \$children_ids){
						
	                    \$list = think\Db::name('product')->where('cid','in',\$children_ids)->where(['status'=>1]);
						unset(\$children_ids);//释放变量
						
	                }
					if("$field" != ''){
					    \$list=\$list->field("$field");
					}
					if("$order" != ''){
					    \$list=\$list->order("$order");
					}
					if("$pagesize" > 0){
					    \$list=\$list->paginate("$pagesize");
					    \$page = \$list->render();
					}else{
					    if("$limit" != 0){
							\$list=\$list->limit("$limit");
					    }
					    \$list=\$list->select();
					}
					
	                \$__LIST__ = \$list;
					
	                foreach(\$__LIST__ as \$key => {$return}):
	                ?>
EOF;
        $parse .= $content;
        $parse .= '<?php endforeach;?>';
        return $parse;
    }
  /**
         * 获取留言
         */
    public function tagGbook($tag, $content)
    {
        $data['cid'] = $cid = isset($tag['cid']) ? $tag['cid'] : 0;
        $data['cache'] = $cache = isset($tag['cache']) ? $tag['cache']:'';
        $data['order'] = $order = isset($tag['orderby']) ? $tag['orderby'] : '';
        $data['limit'] = $limit = isset($tag['limit']) ? $tag['limit'] : 10;
        $data['pagesize'] = $pagesize = isset($tag['pagesize']) ? $tag['pagesize'] : 0;
        $data['field'] = $field = isset($tag['field']) ? $tag['field'] : '';
		$data['return'] = $return = isset($tag['return']) ? '$' .$tag['return'] : '$data';
        $cacheId = to_guid_string(arr_to_html($data));
        $parse = <<<EOF
	        <?php
	                \$list = think\Db::name('guestbook')->where(['status' => 1]);
	                if("$field" != ''){
	                    \$list=\$list->field("$field");
	                }
	                if("$order" != ''){
	                    \$list=\$list->order("$order");
	                }
	                if("$pagesize" > 0){
	                    \$list=\$list->paginate("$pagesize",false,['query' => request()->param()]);
	                    \$page = \$list->render();
	                }else{
	                    if("$limit" != 0){
							\$list=\$list->limit("$limit");
	                    }
	                    \$list=\$list->select();
	                }
	                \$__LIST__ = \$list;
					 $return = \$__LIST__;
	                ?>
EOF;
        $parse .= $content;
        return $parse;
    }
	
	public function tagBlock($tag,$content){
		$data['type'] = $type = isset($tag['type']) ? $tag['type'] : 1;
		$data['cache'] = $cache = isset($tag['cache']) ? $tag['cache']:'';
		$data['return'] = $return = isset($tag['return']) ? '$' .$tag['return'] : '$data';
		$cacheId = to_guid_string(arr_to_html($data));
		$parse = <<<EOF
			<?php
					\$list = think\Db::name('block')->where('id',$type);
					if("$cache" != ''){
						\$list=\$list->cache('$cacheId','$cache','block');
					}
					\$list = \$list->find();
					\$__LIST__ = \$list;
					 $return = \$__LIST__;
					?>
EOF;
		$parse .= $content;
		return $parse;
	}
}
